//
//  PPRequestTableTableViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 9/4/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPRequestTableTableViewController.h"
#import "PPRequestTableViewCell.h"
#import "PPApplyNotification.h"
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"


@interface PPRequestTableTableViewController ()

@property (weak, nonatomic) PPSystemConfiguraton *config;
@property (strong, nonatomic) NSMutableArray *applyArray;


@end

@implementation PPRequestTableTableViewController

-(PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getApply];
}

- (void)getApply{
    self.applyArray = [self.config.applyArray mutableCopy];
    [self.tableView reloadData];
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.applyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PPRequestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"applyCell"];
    PPApplyNotification *apply = [self.applyArray objectAtIndex:indexPath.row];
    cell.userName.text = apply.sendUser.userName;
    cell.message.text = apply.message;
    
    if (apply.operate == 0) {
        cell.agreeButton.hidden = NO;
        cell.refuseButton.hidden = NO;
        cell.resultLabel.hidden = YES;
    }else{
        cell.agreeButton.hidden = YES;
        cell.refuseButton.hidden = YES;
        cell.resultLabel.hidden = NO;
    }
    if (apply.result == 0) {
        cell.resultLabel.text = @"已拒绝";
    }else{
        cell.resultLabel.text = @"已接受";
    }
    
    [cell.agreeButton addTarget:self action:@selector(agree:) forControlEvents:UIControlEventTouchUpInside];
    [cell.refuseButton addTarget:self action:@selector(refuse:) forControlEvents:UIControlEventTouchUpInside];
    cell.agreeButton.tag = indexPath.row;
    cell.refuseButton.tag = indexPath.row;
    
    [cell.userAvatar setImageWithURL:[PPImageUrl getImageUrlOriginal:apply.sendUser.userAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"]usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    return cell;
}

#pragma mark - Table view delete
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - Custom method

-(void)agree:(UIButton *)sender
{
    [self operate:1 andTag:sender.tag];
}

-(void)refuse:(UIButton *)sender
{
    [self operate:0 andTag:sender.tag];
}

- (void)operate:(int)type andTag:(NSInteger)tag
{
    PPApplyNotification *apply = [self.config.applyArray objectAtIndex:tag];
    NSString *url = [PPAPIList getAPIList].PPOperateRequest;
    NSDictionary *params = @{@"requestId":@(apply.id),@"operateType":@(type)};
    [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *message = responseObject[@"errorMsg"];
        [PPHUD hudShowWithString:message andView:self
         .view];
        [self.config getApplyListwithComepletion:^{
            [self getApply];
        }];
        [self.config getFriendListwithComepletion:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [PPHUD hudShowWithString:@"网络不给力" andView:self
         .view];
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
